﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.IO;
using System.Globalization;

namespace Jäckvik_Grupp4
{///GUI för visning av deltagarlistor i skidskolan samt utskrift av dessa.
    public partial class TraineeList : Form
    {
        private LOGIC.LogicSkiclass skiclass = new LOGIC.LogicSkiclass();
        private System.Windows.Forms.PrintDialog printDialog1 = new PrintDialog();
        private System.Drawing.Printing.PrintDocument ThePrintDocument = new PrintDocument();

        public TraineeList()
        {
            InitializeComponent();
            textBox_TListDate.Text = DateTime.Now.Date.ToShortDateString();            
        } 

        /// <summary>
        /// Metod som visar/gömmer listan period beroende på gruppval.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox_PartGroup.SelectedIndex == 4)
            {
                comboBox_PartPeriod.Visible = false;
            }
            else comboBox_PartPeriod.Visible = true;
        }

        /// <summary>
        /// Metod som utifrån de val av grupp och/eller period som görs visar deltagarna i denna.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_PartSearch_Click(object sender, EventArgs e)
        {
            if (comboBox_PartGroup.SelectedIndex == -1)
            {
                MessageBox.Show("Du måste välja en grupp");
            }
            else if (comboBox_PartGroup.SelectedIndex == 4)
            {
                DateTime dt = DateTime.Now;
                string tempDay;

                if (dt.DayOfWeek.ToString() == "Monday")
                { 
                    tempDay = "Måndag";
                    string week = GetWeekFromDate(DateTime.Now).ToString();
                    listBox_TListParticipants.DataSource = skiclass.GetPrivateLessonAttend(tempDay, week);
                }
                else if (dt.DayOfWeek.ToString() == "Tuesday")
                { 
                    tempDay = "Tisdag";
                    string week = GetWeekFromDate(DateTime.Now).ToString();
                    listBox_TListParticipants.DataSource = skiclass.GetPrivateLessonAttend(tempDay, week);
                }
                else if (dt.DayOfWeek.ToString() == "Wednesday")
                { 
                    tempDay = "Onsdag";
                    string week = GetWeekFromDate(DateTime.Now).ToString();
                    listBox_TListParticipants.DataSource = skiclass.GetPrivateLessonAttend(tempDay, week);
                }
                else if (dt.DayOfWeek.ToString() == "Thursday")
                { 
                    tempDay = "torsdag";
                    string week = GetWeekFromDate(DateTime.Now).ToString();
                    listBox_TListParticipants.DataSource = skiclass.GetPrivateLessonAttend(tempDay, week);
                }
                else if (dt.DayOfWeek.ToString() == "Friday")
                { 
                    tempDay = "Fredag";
                    string week = GetWeekFromDate(DateTime.Now).ToString();
                    listBox_TListParticipants.DataSource = skiclass.GetPrivateLessonAttend(tempDay, week);
                }
            }
            else
            {
                if (comboBox_PartPeriod.SelectedIndex == -1)
                {
                    MessageBox.Show("Du måste välja en period");
                }
                else
                {
                    string week = GetWeekFromDate(DateTime.Now).ToString();
                    string group = comboBox_PartGroup.Text;
                    string period = comboBox_PartPeriod.Text;
                    listBox_TListParticipants.DataSource = skiclass.GetGroupLessonAttend(week, period, group);
                }
                
            }
        }

        /// <summary>
        /// Metod som hämtar kontrollerar vecka utifrån ett datum.
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public int GetWeekFromDate(DateTime date)
        {
            CultureInfo culture = new CultureInfo("sv-SE");
            int week = culture.Calendar.GetWeekOfYear(date, CalendarWeekRule.FirstFullWeek, DayOfWeek.Sunday);
            return week;
        }

        /// <summary>
        /// Metod som skriver ut formuläret (deltagarlista)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_Ski_Print_Click(object sender, EventArgs e)
        {
            PrintPreviewDialog dlg = new PrintPreviewDialog();
            dlg.Document = ThePrintDocument;
            dlg.ShowDialog();
        }

        /// <summary>
        /// Metod som tar in formuläret som en bild för utskrift. 
        /// </summary>
        /// <param name="o"></param>
        /// <param name="e"></param>
        void PrintImage(object o, PrintPageEventArgs e)
        {
            int x = SystemInformation.WorkingArea.X;
            int y = SystemInformation.WorkingArea.Y;
            int width = this.Width;
            int height = this.Height;

            Rectangle bounds = new Rectangle(x, y, width, height);

            Bitmap img = new Bitmap(width, height);

            this.DrawToBitmap(img, bounds);
            Point p = new Point(100, 100);
            e.Graphics.DrawImage(img, p);
        }

      
        
    }
}
